package com.egoo.ticket.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.egoo.ticket.server.config.mq.MqCommonUtil;
import com.egoo.ticket.server.project.jincheng.JinchengTimeOutJudge;
import com.egoo.ticket.server.service.EurmDataSyncService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping(value = "/v1/EurmDataSyn", produces = "application/json")
@Api(value = "内管数据对接管理",tags = "内管数据对接管理",description = "内管数据对接管理")
public class EurmDataSyncController {

	@Autowired
	private EurmDataSyncService eurmDataSyncService;
	@Autowired
	private MqCommonUtil mqCommonUtil;
	@Autowired
	JinchengTimeOutJudge jinchengTimeOutJudge;

	@ApiOperation(value = "提供给内管，触发同步用户，角色，部门数据", notes = "提供给内管，触发同步用户，角色，部门数据")
	@RequestMapping(method = RequestMethod.POST, value = "/syncEurmData")
	public DbResponse syncEurmData(@RequestBody JSONObject jsonObject) {
		log.info("内管推送的的同步数据为："+jsonObject.toJSONString());
		if (jsonObject==null) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "内管传来的同步数据为空");
		}
		JSONObject info = jsonObject.getJSONObject("info");
		String dataType = info.getString("dataType");
		if (ConstantCode.SynEurmDataType.USER.equals(dataType)) {
			return eurmDataSyncService.syncUser(jsonObject);
		}
		else if (ConstantCode.SynEurmDataType.ROLE.equals(dataType)) {
			return eurmDataSyncService.syncRole(jsonObject);
		}
		else if (ConstantCode.SynEurmDataType.DEPT_GROUP.equals(dataType)) {
			return eurmDataSyncService.syncDept(jsonObject);
		}else if (ConstantCode.SynEurmDataType.ORG.equals(dataType)){
			return eurmDataSyncService.syncTenant(jsonObject);
		}
		else{
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,
					"此dataType不是工单系统同步的数据类型，请确认！");
		}
	}
	@ApiOperation(value = "获取内管数据对象总数", notes = "获取内管数据对象总数")
	@RequestMapping(method = RequestMethod.GET, value = "/getEurmCountByDataType")
	public DbResponse getEurmCountByDataType(@ApiParam(value = "dataType", required = true)
												 @RequestParam(name = "dataType")String dataType) {
		log.info("获取内管数据对象总数的dataType为："+dataType);
		Integer result = eurmDataSyncService.getEurmCountByDataType(dataType,null);
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,
				ResponseConstant.SELECT_SUCCESS_MSG,result);
	}
	@ApiOperation(value = "批量同步租户用户，角色，部门信息", notes = "批量同步租户用户，角色，部门信息")
	@RequestMapping(method = RequestMethod.GET, value = "/batchSynTenantBaseInfo")
	public DbResponse batchSynTenantBaseInfo(@ApiParam(value = "tenantIds", required = true)
												 @RequestParam(name = "tenantIds")String tenantIds) {
		if (StringUtils.isEmpty(tenantIds)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"租户id不能为空！");
		}
		List<String> tenantIdList = Arrays.asList(tenantIds.split(","));
		tenantIdList.forEach(v->{
			eurmDataSyncService.batchPushSynPramaToMq(v, ConstantCode.SynEurmDataType.USER);
			eurmDataSyncService.batchPushSynPramaToMq(v, ConstantCode.SynEurmDataType.ROLE);
			eurmDataSyncService.batchPushSynPramaToMq(v, ConstantCode.SynEurmDataType.DEPT_GROUP);
		});
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE, "同步数据命令发送成功，请稍等2分钟！");
	}

	@ApiOperation(value = "获取节假日", notes = "获取节假日")
	@RequestMapping(method = RequestMethod.GET, value = "/getRestDay")
	public DbResponse getRestDay() {
		List<Integer> moreYear = jinchengTimeOutJudge.getMoreYear();
		List<String> restDateList = new ArrayList<>();
		for (Integer year:moreYear
		) {
			List<String> dateList = eurmDataSyncService.getRestDay(year);
			restDateList.addAll(dateList);
		}



		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE, "ok",restDateList);
	}

	@ApiOperation(value = "根据新建租户创建数据库铺地数据", notes = "根据新建租户创建数据库铺地数据")
	@RequestMapping(method = RequestMethod.POST, value = "/initDB")
	public DbResponse initDBData(@RequestBody JSONObject jsonObject) {

		log.info("内管推送的租户信息："+jsonObject);
		String tenantId = jsonObject.getString("tenantId");
		List<String> arrayList = new ArrayList<>();
		arrayList.add(tenantId);
		return eurmDataSyncService.initDBData(arrayList);

	}
	@ApiOperation(value = "获取内管租户", notes = "获取内管租户")
	@RequestMapping(method = RequestMethod.GET, value = "/getTenant")
	public DbResponse getTenant() {
		List<String> set = eurmDataSyncService.getTenant();
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,ResponseConstant.SELECT_SUCCESS_MSG,set);

	}

	@ApiOperation(value = "异常测试", notes = "异常测试")
	@RequestMapping(method = RequestMethod.GET, value = "/exceptionTest")
	public DbResponse exceptionTest() {
		Integer s = 1/0;
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,ResponseConstant.SELECT_SUCCESS_MSG);

	}
}
